/*!
 * Ext JS Library 3.4.0
 * Copyright(c) 2006-2011 Sencha Inc.
 * licensing@sencha.com
 * http://www.sencha.com/license
 */
Ext.chart.Chart.CHART_URL = '/ad/html/publish/js/charts.swf';
var store=null;
var store2=null;
var qs =  new QueryString();
var id = qs.get("id");
var status=0;
var panel=null;
var panel2=null;
var type=0;

var storeReload=function(date){
	 store.reload(
			 {params:{
				"id" : id,
	 			"date" : date,
	 			"status" : status
				 }
			 }
			 ); 
	 panel.doLayout();
};
var storeReload2=function(date){
	 store2.reload(
			 {params:{
				"id" : id,
	 			"date" : date,
	 			"status" : status
				 }
			 }
			 ); 
	 panel.doLayout();
};
Ext.onReady(function(){
	var prev=new Ext.Toolbar.Button ({   
        text:"上個月",    
         minWidth:100,   
         handler:function(){   
        	 var dateSplit=Ext.get("dateValue").getValue().split("-");
        	 var myDate=new Date();
        	 myDate.setFullYear(dateSplit[0],dateSplit[1]-2,1);
        	 Ext.get("dateValue").dom.value=myDate.format('Y-m');
        	 Ext.get("dateValue2").dom.value=myDate.format('Y-m');
        	 switch(type){
        	 case 0:case 1:storeReload(myDate.format('Y-m'));break;
        	 case 2:case 3:storeReload2(myDate.format('Y-m'));break;
        	 }
//        	 storeReload(myDate.format('Y-m'));
        	 
         }   
	});    
	var next=new Ext.Toolbar.Button ({   
        text:"下個月",    
         minWidth:100,    
         handler:function(){   
        	 var dateSplit=Ext.get("dateValue").getValue().split("-");
        	 var myDate=new Date();
        	 myDate.setFullYear(dateSplit[0],dateSplit[1],1);
        	 Ext.get("dateValue").dom.value=myDate.format('Y-m');
        	 Ext.get("dateValue2").dom.value=myDate.format('Y-m');
        	 switch(type){
        	 case 0:case 1:storeReload(myDate.format('Y-m'));break;
        	 case 2:case 3:storeReload2(myDate.format('Y-m'));break;
        	 }
         }   
	});   
	var exportButton=new Ext.Toolbar.Button ({   
        text:"匯出至CSV",    
         minWidth:100,    
         handler:function(){   
        	var html="/ad/sys/com/control/publish/ClickedAdService.php?action=exportExcel";
        	html+="&type="+type;
        	html+="&id="+id;
        	html+="&status="+status;
        	html+="&date="+Ext.get("dateValue").dom.value;
        	window.open (html, 'export', 'height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no, status=no');
         }   
	});
	var prev2=new Ext.Toolbar.Button ({   
        text:"上個月",    
         minWidth:100,   
         handler:function(){   
        	 var dateSplit=Ext.get("dateValue").getValue().split("-");
        	 var myDate=new Date();
        	 myDate.setFullYear(dateSplit[0],dateSplit[1]-2,1);
        	 Ext.get("dateValue").dom.value=myDate.format('Y-m');
        	 Ext.get("dateValue2").dom.value=myDate.format('Y-m');
        	 switch(type){
        	 case 0:case 1:storeReload(myDate.format('Y-m'));break;
        	 case 2:case 3:storeReload2(myDate.format('Y-m'));break;
        	 }
//        	 storeReload(myDate.format('Y-m'));
        	 
         }   
	});    
	var next2=new Ext.Toolbar.Button ({   
        text:"下個月",    
         minWidth:100,    
         handler:function(){   
        	 var dateSplit=Ext.get("dateValue").getValue().split("-");
        	 var myDate=new Date();
        	 myDate.setFullYear(dateSplit[0],dateSplit[1],1);
        	 Ext.get("dateValue").dom.value=myDate.format('Y-m');
        	 Ext.get("dateValue2").dom.value=myDate.format('Y-m');
        	 switch(type){
        	 case 0:case 1:storeReload(myDate.format('Y-m'));break;
        	 case 2:case 3:storeReload2(myDate.format('Y-m'));break;
        	 }
         }   
	});   
	var exportButton2=new Ext.Toolbar.Button ({   
        text:"匯出至CSV",    
         minWidth:100,    
         handler:function(){   
        	var html="/ad/sys/com/control/publish/ClickedAdService.php?action=exportExcel";
        	html+="&type="+type;
        	html+="&id="+id;
        	html+="&status="+status;
        	html+="&date="+Ext.get("dateValue").dom.value;
        	window.open (html, 'export', 'height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no, status=no');
         }   
	});   
	
	
	
	
    store = new Ext.data.JsonStore({
    	 proxy: new Ext.data.HttpProxy({  
            method: 'GET',  
            url:"/ad/sys/com/control/publish/ClickedAdService.php?action=getStatisticsCountDate"
//            ,extraParams: {"dateEnd" : new Date().format('Y-m-d')}  
        }),
		root : "row", 
		remoteSort: false,
		totalProperty: 'count',
		fields : ["Date", "Sum"]
    });
    store2 = new Ext.data.JsonStore({
   	 proxy: new Ext.data.HttpProxy({  
           method: 'GET',  
           url:"/ad/sys/com/control/publish/ClickedAdService.php?action=getStatisticsRatioDate"
//           ,extraParams: {"dateEnd" : new Date().format('Y-m-d')}  
       }),
		root : "row", 
		remoteSort: false,
		totalProperty: 'count',
		fields : ["Date", "Sum"]
   });  
       
    store.load(
    		{
    			params:{
    				"id" : id,
    				"date" : new Date().format('Y-m'),
    				"status" : 0
    				
    			}
    		});
    
    store2.load(
    		{
    			params:{
    				"id" : id,
    				"date" : new Date().format('Y-m'),
    				"status" : 0
    				
    			}
    		});
    // extra simple
    panel=new Ext.Panel({
    	id:"panel",
        iconCls:'chart',
        frame:false,
        renderTo: 'container',
        width:879,
        height:230,
        layout:'fit',
        bbar:{
        	xtype:'toolbar',
        	items:[
        		// searchTB,
        		prev
        		,"<input id='dateValue' type='text' readonly value='"+new Date().format('Y-m')+"' onchage='dateValueChange()'/>"
        		,next
//        		dobFieldEditor,
        		,exportButton
        		]
        	},
        items: {
            xtype: 'linechart',
            store: store,
            url: Ext.chart.Chart.CHART_URL,
            xField: 'Date',
            yField: 'Sum',
//            yAxis: new Ext.chart.NumericAxis({
//                displayName: 'Sum',
//                labelRenderer : Ext.util.Format.numberRenderer('0,0')
//            }),
            tipRenderer : function(chart, record){
                return Ext.util.Format.number(record.data.Sum, '0,0') + ' visits in ' + record.data.Date;
            }
        }
    });
    panel2=new Ext.Panel({
    	id:"panel2",
        iconCls:'chart',
        frame:false,
        renderTo: 'container2',
        width:879,
        height:230,
        layout:'fit',
        bbar:{
        	xtype:'toolbar',
        	items:[
        		// searchTB,
        		prev2
        		,"<input id='dateValue2' type='text' readonly value='"+new Date().format('Y-m')+"' />"
        		,next2
//        		dobFieldEditor,
        		,exportButton2
        		]
        	},
        items: {
            xtype: 'linechart',
            store: store2,
            url: Ext.chart.Chart.CHART_URL,
            xField: 'Date',
            yField: 'Sum',
//            yAxis: new Ext.chart.NumericAxis({
//                displayName: 'Sum',
//                labelRenderer : Ext.util.Format.numberRenderer('0,0')
//            }),
            tipRenderer : function(chart, record){
                return Ext.util.Format.number(record.data.Sum, '0,0') + ' % in ' + record.data.Date;
            }
        }
    });

});